package com.specter.utils;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * Note:只限于本框架的内部使用
 * 
 * @author Liang.Wang
 * @version May 21, 2017
 */
public class CookieUtils {

	/**
	 * 设置相应的cookie
	 */
	public static void addAESCookie(HttpServletResponse response, String cookieName, String cookieValue) {
		addAESCookie(response, cookieName, cookieValue, null, "/", -1);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addAESCookie(HttpServletResponse response, String cookieName, String cookieValue, int maxAge) {
		addAESCookie(response, cookieName, cookieValue, null, "/", maxAge);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addAESCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain) {
		addAESCookie(response, cookieName, cookieValue, domain, "/", -1);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addAESCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain, int maxAge) {
		addAESCookie(response, cookieName, cookieValue, domain, "/", maxAge);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addAESCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain, String path) {
		addAESCookie(response, cookieName, cookieValue, domain, path, -1);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addAESCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain, String path, int maxAge) {
		Cookie cookie = new Cookie(cookieName, AESUtils.encrypt(cookieValue));
		if (maxAge != -1) {
			cookie.setMaxAge(maxAge);
		}
		if (!StringUtils.isBlank(path)) {
			cookie.setPath(path);
		}
		if (!StringUtils.isBlank(domain)) {
			cookie.setDomain(domain);
		}
		response.addCookie(cookie);// 清除cookie
	}

	/**
	 * 按照cookie的名字获取值(加密)
	 */
	public static String getAESCookie(HttpServletRequest request, String cookieName) {
		String cookieValue = getCookie(request, cookieName);
		if (cookieValue != null) {
			return AESUtils.decrypt(cookieValue);
		}
		return cookieValue;
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue) {
		addCookie(response, cookieName, cookieValue, null, "/", -1);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, int maxAge) {
		addCookie(response, cookieName, cookieValue, null, "/", maxAge);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain) {
		addCookie(response, cookieName, cookieValue, domain, "/", -1);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain, int maxAge) {
		addCookie(response, cookieName, cookieValue, domain, "/", maxAge);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain, String path) {
		addCookie(response, cookieName, cookieValue, domain, path, -1);
	}

	/**
	 * 设置相应的cookie
	 */
	public static void addCookie(HttpServletResponse response, String cookieName, String cookieValue, String domain, String path, int maxAge) {
		Cookie cookie = new Cookie(cookieName, cookieValue);
		if (maxAge != -1) {
			cookie.setMaxAge(maxAge);
		}
		if (!StringUtils.isBlank(path)) {
			cookie.setPath(path);
		}
		if (!StringUtils.isBlank(domain)) {
			cookie.setDomain(domain);
		}
		response.addCookie(cookie);// 清除cookie
	}

	/**
	 * 按照cookie的名字获取值
	 */
	public static boolean existsNotCookie(HttpServletRequest request, String cookieName) {
		return !existsCookie(request, cookieName);
	}

	/**
	 * 按照cookie的名字获取值
	 */
	public static boolean existsCookie(HttpServletRequest request, String cookieName) {
		return getCookie(request, cookieName) != null;
	}

	/**
	 * 按照cookie的名字获取值
	 */
	public static String getCookie(HttpServletRequest request, String cookieName) {
		Cookie[] cookies = request.getCookies();// 这样便可以获取一个cookie数组
		if (cookies == null) {
			return null;
		}
		for (Cookie cookie : cookies) {
			if (cookieName.equals(cookie.getName())) {
				return cookie.getValue();
			}
		}
		return null;
	}

	/**
	 * 删除对应的cookie
	 */
	public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName) {
		removeCookie(request, response, cookieName, "/");
	}

	/**
	 * 删除对应的cookie
	 */
	public static void removeCookie(HttpServletRequest request, HttpServletResponse response, String cookieName, String path) {
		Cookie[] cookies = request.getCookies();
		if (cookies == null) {
			return;
		}
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(cookieName)) {
				Cookie cookie = new Cookie(cookieName, null);
				cookie.setMaxAge(0);
				if (!StringUtils.isBlank(path)) {
					cookie.setPath(path);
				}
				response.addCookie(cookie);// 清除cookie
				return;
			}
		}
	}

}
